% evaluates moment conditions of the form E[h(x,r)] = p 
% aJ is a vector of zeros and ones that selects moments to be evaluated
% set aJ = 0 to use S matrix as covar of pricing errors in case where no
% parameters are estimated (in this case the function always uses all moments) 
% v is a NxNxT matrix of T covariance matrices, each NxN (N=# factors), or -1 if it's to be ignored

function [g] = GMMcritg(gtfcn,b,v,x,r,p,W,h,Sfcn,aJ,wt)


t = size(r,1);
if nargin > 8 && size(aJ,1) > 1    % size(aJ,1) == 1 : no SDF parameters estimated
    fast = 0; 
else
    fast = 1; 
end

if nargin < 8
    h = 0; 
end 

[gt, dg] = gtfcn(b,v,x,r,p,h,fast); 
g = mean(gt);

